/*
 * pps_bom
 */
package com.pitop.project.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.pitop.core.base.BaseDAO;
import com.pitop.core.base.BaseSVImpl;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.ProjectBomException;
import com.pitop.core.exceptions.ProjectStaffException;
import com.pitop.core.tools.StringTools;
import com.pitop.project.dao.ProjectStaffDAO;
import com.pitop.project.entity.ProjectBom;
import com.pitop.project.entity.ProjectStaff;
import com.pitop.project.entity.ProjectStaffState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("projectStaffSV")
@Slf4j
public class ProjectStaffSVImpl extends BaseSVImpl<ProjectStaff, Long> implements ProjectStaffSV {

    @Autowired
    private ProjectStaffDAO projectStaffDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return projectStaffDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(ProjectStaff entity) throws BaseException {
        if (entity == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringTools.isEmpty(entity.getProjectCode())) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (StringTools.isEmpty(entity.getAccountCode())) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //批量添加projectBom
        List<ProjectStaff> projectStaffList = new ArrayList<>();
        ProjectStaff projectStaff;
        String[] accountCodeAry = entity.getAccountCode().split(",");
        for (String accountCode : accountCodeAry) {
            projectStaff = new ProjectStaff();
            projectStaff.setProjectCode(entity.getProjectCode());
            projectStaff.setAccountCode(accountCode);
            projectStaffList.add(projectStaff);
        }
        projectStaffDAO.batchInsert(projectStaffList);
    }

    /**
     * 加载一个对象ProjectStaff
     *
     * @param id
     * @param projectCode 项目编码
     * @param accountCode 员工编码
     * @return ProjectStaff
     */
    @Override
    public ProjectStaff load(Long id, String projectCode, String accountCode) {
        if (id == null && projectCode == null && accountCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("accountCode", accountCode);
        return projectStaffDAO.load(param);
    }


    /**
     * 加载一个对象ProjectStaff 通过id
     *
     * @param id
     * @return ProjectStaff
     */
    @Override
    public ProjectStaff loadById(Long id) {
        if (id == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectStaffDAO.loadById(id);
    }

    /**
     * 加载一个对象ProjectStaff 通过projectCode
     *
     * @param projectCode 项目编码
     * @return ProjectStaff
     */
    @Override
    public ProjectStaff loadByProjectCode(String projectCode) {
        if (projectCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectStaffDAO.loadByProjectCode(projectCode);
    }

    /**
     * 加载一个对象ProjectStaff 通过accountCode
     *
     * @param accountCode 员工编码
     * @return ProjectStaff
     */
    @Override
    public ProjectStaff loadByAccountCode(String accountCode) {
        if (accountCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectStaffDAO.loadByAccountCode(accountCode);
    }


    /**
     * 根据主键id,oldStates 共同更新 ProjectStaff 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(Long id, ProjectStaffState newState, ProjectStaffState... oldStates) {
        if (id == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectStaffDAO.updateStateById(id, new Date(), newState.name(), states);
    }

    /**
     * 根据主键projectCode,oldStates 共同更新 ProjectStaff 的状态到newState状态
     *
     * @param projectCode 项目编码
     * @param newState    新状态
     * @param oldStates   旧状态集合
     */
    @Override
    public void updateStateByProjectCode(String projectCode, ProjectStaffState newState, ProjectStaffState... oldStates) {
        if (projectCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectStaffDAO.updateStateByProjectCode(projectCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键accountCode,oldStates 共同更新 ProjectStaff 的状态到newState状态
     *
     * @param accountCode 员工编码
     * @param newState    新状态
     * @param oldStates   旧状态集合
     */
    @Override
    public void updateStateByAccountCode(String accountCode, ProjectStaffState newState, ProjectStaffState... oldStates) {
        if (accountCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (newState == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        if (oldStates == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        String[] states = new String[oldStates.length];
        for (int i = 0; i < oldStates.length; i++) {
            states[i] = oldStates[i].name();
        }
        projectStaffDAO.updateStateByAccountCode(accountCode, new Date(), newState.name(), states);
    }

    /**
     * 根据主键id 更新 ProjectStaff 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(Long id, ProjectStaffState state) {
        if (id == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectStaffDAO.updateById(id, state.name(), new Date());
    }

    /**
     * 根据主键projectCode 更新 ProjectStaff 的状态到另一个状态
     *
     * @param projectCode 项目编码
     * @param state       状态
     */
    @Override
    public void updateByProjectCode(String projectCode, ProjectStaffState state) {
        if (projectCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectStaffDAO.updateByProjectCode(projectCode, state.name(), new Date());
    }

    /**
     * 根据主键accountCode 更新 ProjectStaff 的状态到另一个状态
     *
     * @param accountCode 员工编码
     * @param state       状态
     */
    @Override
    public void updateByAccountCode(String accountCode, ProjectStaffState state) {
        if (accountCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectStaffDAO.updateByAccountCode(accountCode, state.name(), new Date());
    }


    /**
     * 删除对象ProjectStaff
     *
     * @param id * @param projectCode 项目编码* @param accountCode 员工编码
     * @return ProjectStaff
     */
    @Override
    public void delete(Long id, String projectCode, String accountCode) {
        if (id == null && projectCode == null && accountCode == null) {
            throw new ProjectStaffException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("accountCode", accountCode);
        projectStaffDAO.delete(param);
    }


    /**
     * 查询ProjectStaff分页
     *
     * @param projectStaff 对象
     * @param offset       查询开始行
     * @param limit        查询行数
     * @return List<ProjectStaff>
     */
    @Override
    public List<ProjectStaff> list(ProjectStaff projectStaff, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (projectStaff != null) {
            map = JSON.parseObject(JSON.toJSONString(projectStaff, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return projectStaffDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(ProjectStaff projectStaff) {
        Map<String, Object> map = null;
        if (projectStaff != null) {
            map = JSON.parseObject(JSON.toJSONString(projectStaff, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return projectStaffDAO.count(map);
    }

    /**
     * 查询ProjectStaff分页
     *
     * @param id     * @param projectCode  项目编码* @param accountCode  员工编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<ProjectStaff>
     */
    @Override
    public List<ProjectStaff> list(Long id, String projectCode, String accountCode, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("accountCode", accountCode);
        return projectStaffDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id, String projectCode, String accountCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("projectCode", projectCode);
        param.put("accountCode", accountCode);
        return projectStaffDAO.count(param);
    }

}
